/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"encoding/json"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"github.com/valyala/fasthttp"
	"saas_sys_base/common"
	. "saas_sys_base/modules/appmanager/models"
	. "saas_sys_base/modules/appmanager/service"
	configService "saas_sys_base/modules/config/service"
)

type AppManagerApiCtrl struct {
	appManagerService AppManagerService
	appConfigService  configService.AppConfigService
}

func init() {
	vc := new(AppManagerApiCtrl)
	_ctrlList = append(_ctrlList, vc)

	/*appconfigList := vc.appConfigService.GetAllAppConfig()
	for _, appconfig := range appconfigList {
		vc.appManagerService.RegisterApp(appconfig)
	}*/
}

func (vc *AppManagerApiCtrl) CtrlInfo() string {
	return "AppAttrCtrl"
}

func (vc *AppManagerApiCtrl) RoutInit() {
	lego.APIPOST("/api/sys/appmanager/add_apppermission_expiretime", vc.AddAppPermissionExpireTime, true)
}

func (vc *AppManagerApiCtrl) AddAppPermissionExpireTime(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	//data := make(map[string]interface{})
	//sessionData := common.GetSessionDataByCtx(&ctxExt)
	type Resp struct {
		AppTenantPermission *AppTenantPermission
	}
	code := 0
	msg := "success"

	for {
		//BODY内json数据的定义
		type Request struct {
			Tid   int64 `json:"tid"`
			AppId int64 `json:"appid"` ///内部app id, internal app id
			//Uid   int64 `json:"uid"`

			Days int `json:"days" vd:"$>0"`
		}

		//url querystring格式定义
		type UrlParam struct {
			/*Code    string `urlenc:"code"`
			Tid    int64 `urlenc:"tid"`
			AppId int64 `urlenc:"appid"`*/
		}

		var req Request
		var urlParam UrlParam
		if err := ctxExt.UrlDecode(&urlParam); err != nil {
			code = 100
			msg = err.Error()
			break
		}
		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &req)
		//req.Tid = utils.BytesToInt64(ctxExt.UrlPeek("tid")) //tenant id
		if err != nil {
			code = 100
			msg = err.Error()
			break
		}

		if err := lego.Validate(req); err != nil {
			code = 100
			msg = err.Error()
			break
		}

		var appwho common.Who
		appwho.TenantId = req.Tid
		appwho.MiniAppId = req.AppId
		//appwho.Uid = req.Uid

		ymd := utils.YearMonthDays{
			Days: req.Days,
		}


		//先查询有没有这个租户
		//who := common.NewWho(req.Tid, req.AppId, 0)
		appTenantPermission, err := vc.appManagerService.AddAppPermissionForExpireTime(&appwho, appwho.MiniAppId, ymd) //TODO 返回值加设置的权限记录
		if err != nil {
			code = 100
			msg = err.Error()
			break
		}

		resp := &Resp{}
		resp.AppTenantPermission = appTenantPermission
		response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	ctxExt.JSON(200, response)
	return
}
